//
//  FoodViewController.m
//  HealthA段
//
//  Created by dllo on 15/6/3.
//  Copyright (c) 2015年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "FoodViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIImageView+WebCache.h"
#import "FoodCell.h"
#import "Food.h"
#import "BeautyFaceViewController.h"
#import "loseweightViewController.h"
#import "healthcareViewController.h"
#import "FitpeopleViewController.h"
#import "seasonViewController.h"
#import "pregnancyAndBirthViewController.h"
#import "FoodDatilViewController.h"
#import "Singleton.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface FoodViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>
@property(nonatomic,retain)UITableView *foodTable;
@property(nonatomic,retain)NSMutableArray *foodArr;
@property(nonatomic,retain)UIScrollView *headScroll;
@property(nonatomic,retain)UIViewController *currentView;
@property(nonatomic,retain)NSArray *headArr;
@property(nonatomic,retain)BeautyFaceViewController *bvc;
@property(nonatomic,retain)loseweightViewController *lvc;
@property(nonatomic,retain)healthcareViewController *hvc;
@property(nonatomic,retain)FitpeopleViewController *fvc;
@property(nonatomic,retain)seasonViewController *svc;
@property(nonatomic,retain)pregnancyAndBirthViewController *pvc;
@property(nonatomic,retain)UIActivityIndicatorView *juhua;
@end

@implementation FoodViewController
- (void)dealloc
{
    [_foodTable release];
    [_headArr release];
    [_headScroll release];
    [_currentView release];
    [_foodArr release];
    [super dealloc];
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.foodArr = [NSMutableArray array];
        self.headArr = [NSArray array];
//    [self PleaseData];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    
    [[Singleton shareSingleton]hiddenNO];
    
    [self creatView];
    [self creatHeadView];
    [self PleaseData];
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-xuanzexuanxiang"] style:UIBarButtonItemStylePlain target:self action:@selector(clickDown)];
    
    
    
}
//navigationController.view 隐藏问题
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.headScroll.hidden = NO;
    self.tabBarController.tabBar.hidden = NO;

}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.headScroll.hidden = YES;
//    self.tabBarController.tabBar.hidden = YES;
}
//创建滑动试图
-(void)creatHeadView
{
    self.headArr = @[@"美容",@"养颜",@"美白",@"润肤",@"护眼",@"排毒"];
    
    self.headScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
    [self.view sendSubviewToBack:self.headScroll];
    
    self.headScroll.backgroundColor = [UIColor colorWithRed:69/256.0 green:188/256.0 blue:153/256.0 alpha:1];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.headScroll.contentSize = CGSizeMake(2 * WIDTH, 0);
    self.headScroll.bounces = NO;
    self.headScroll.pagingEnabled = YES;
    [self.navigationController.view addSubview:self.headScroll];
    
    for (int i = 0; i < [self.headArr count]; i++) {
        UIButton *bu = [UIButton buttonWithType:UIButtonTypeSystem];
        bu.frame = CGRectMake(0 + i * 0.33 * WIDTH, 20, 0.3 * WIDTH, 44);
        [bu setTitle:[self.headArr objectAtIndex:i] forState:UIControlStateNormal];
        bu.tag = i + 1000;
        [bu setTintColor:[UIColor blackColor]];
        [[self.navigationController.view viewWithTag:1000]setTintColor:[UIColor purpleColor]];
        [bu addTarget:self action:@selector(clickBu:) forControlEvents:UIControlEventTouchUpInside];
        [self.headScroll addSubview:bu];
        
        self.bvc = [[BeautyFaceViewController alloc]init];
        [self.bvc.view setFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        [self addChildViewController:_bvc];
        
        self.lvc = [[loseweightViewController alloc]init];
        [self.lvc.view setFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        
        self.hvc = [[healthcareViewController alloc]init];
        [self.hvc.view setFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        
        self.fvc = [[FitpeopleViewController alloc]init];
        [self.fvc.view setFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        
        self.svc = [[seasonViewController alloc]init];
        [self.svc.view setFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        
        self.pvc = [[pregnancyAndBirthViewController alloc]
                    init];
        [self.pvc.view setFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    
        //默认第一个试图
        [self.view addSubview:self.bvc.view];
        self.currentView = self.bvc;
    }
    
}

-(void)clickBu:(UIButton *)bu
{
    if ((self.currentView == self.bvc && bu.tag == 1000) || (self.currentView == self.lvc && bu.tag == 1001) || (self.currentView == self.hvc && bu.tag == 1002) || (self.currentView == self.fvc && bu.tag == 1003) || (self.currentView == self.svc && bu.tag == 1004) || (self.currentView == self.pvc && bu.tag == 1005)) {
        return;
    }else{
        switch (bu.tag) {
            case 1000:
                [self replaceController:self.currentView newController:self.bvc];
                [self ChangeButtonFontAndColorTag1:1000 tag2:1001 tag3:1002 tag4:1003 tag5:1004 tag6:1005];
                break;
                case 1001:
                [self replaceController:self.currentView newController:self.lvc];
                [self ChangeButtonFontAndColorTag1:1001 tag2:1000 tag3:1002 tag4:1003 tag5:1004 tag6:1005];
                break;
                case 1002:
                [self replaceController:self.currentView newController:self.hvc];
                [self ChangeButtonFontAndColorTag1:1002 tag2:1001 tag3:1000 tag4:1003 tag5:1004 tag6:1005];
                break;
                case 1003:
                [self replaceController:self.currentView newController:self.fvc];
                [self ChangeButtonFontAndColorTag1:1003 tag2:1001 tag3:1002 tag4:1000 tag5:1004 tag6:1005];
                break;
                case 1004:
                [self replaceController:self.currentView newController:self.svc];
                [self ChangeButtonFontAndColorTag1:1004 tag2:1001 tag3:1002 tag4:1003 tag5:1000 tag6:1005];
                break;
                case 1005:
                [self replaceController:self.currentView newController:self.pvc];
                [self ChangeButtonFontAndColorTag1:1005 tag2:1001 tag3:1002 tag4:1003 tag5:1004 tag6:1000];
                break;
        }
        
    }
}

- (void)replaceController:(UIViewController *)oldController newController:(UIViewController *)newController
{
    [self addChildViewController:newController];
    [self transitionFromViewController:oldController toViewController:newController duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished) {
        if (finished) {
            [newController didMoveToParentViewController:self];
            [oldController willMoveToParentViewController:nil];
            [oldController removeFromParentViewController];
            self.currentView = newController;
        }else{
            self.currentView = oldController;
        }
        
    }];
}

- (void)ChangeButtonFontAndColorTag1:(NSInteger)tag1 tag2:(NSInteger)tag2 tag3:(NSInteger)tag3 tag4:(NSInteger)tag4 tag5:(NSInteger)tag5 tag6:(NSInteger)tag6
{
    [[self.navigationController.view viewWithTag:tag1] setTintColor:[UIColor purpleColor]];
    [[self.navigationController.view viewWithTag:tag2] setTintColor:[UIColor blackColor]];
    [[self.navigationController.view viewWithTag:tag3] setTintColor:[UIColor blackColor]];
    [[self.navigationController.view viewWithTag:tag4] setTintColor:[UIColor blackColor]];
    [[self.navigationController.view viewWithTag:tag5] setTintColor:[UIColor blackColor]];
    [[self.navigationController.view viewWithTag:tag6] setTintColor:[UIColor blackColor]];

}
//创建视图
-(void)creatView
{
    self.foodTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 - 49) style:UITableViewStylePlain];
    self.foodTable.rowHeight = 120;
    self.foodTable.delegate =self;
    self.foodTable.dataSource = self;
    self.foodTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_foodTable];
    [_foodTable release];
}

#pragma mark 协议方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.foodArr.count > 0) {
        [self.juhua stopAnimating];
        [self.juhua setHidesWhenStopped:YES];
    }
    return self.foodArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    FoodCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (cell == nil) {
        cell = [[[FoodCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse]autorelease];
    }
    Food *f = self.foodArr[indexPath.row];
    NSString *str = @"http://www.yi18.net/";
    NSString *pivUrl = [str stringByAppendingString:f.img];
    NSURL *url = [NSURL URLWithString:pivUrl];
    [cell.picView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"占位图"]];
    cell.nameLabel.text = f.name;
    cell.foodLabel.text = f.food;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

//数据请求
-(void)PleaseData
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager  GET:@"http://api.yi18.net/cook/list?page=1&limit=20" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *arr = [dic objectForKey:@"yi18"];
        for (NSMutableDictionary *tep in arr) {
            Food *f = [[Food alloc]init];
            [f setValuesForKeysWithDictionary:tep];
            [self.foodArr addObject:f];
            [f release];
        }
        if (self.foodArr.count > 0) {
            [[Singleton shareSingleton] hiddenYes];
        }
        [self.foodTable reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败请求");
    }];
}

//跳转详情页面
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FoodDatilViewController *fvc = [[FoodDatilViewController alloc]
                                init];
    Food *f = self.foodArr[indexPath.row];
    fvc.title = f.name;
    fvc.id = f.id;
//    NSLog(@"id ===%ld",f.id);
    [self.navigationController pushViewController:fvc animated:YES];
    
    
    [fvc release];
}
-(void)clickDown
{
    [UIView animateWithDuration:1.0 animations:^{
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 120, 300)];
        [self.view addSubview:view];
        view.backgroundColor = [UIColor yellowColor];
        [view release];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
